package com.ruoyi.web.controller.uav;

import java.util.Arrays;
import java.util.List;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.constant.CommonConstants;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.uav.domain.UavOperationExercise;
import com.ruoyi.uav.service.IUavOperationExerciseService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import javax.annotation.Resource;

/**
 * 操作练习Controller
 *
 * @author ruoyi
 * @date 2024-01-04
 */
@RestController
@RequestMapping("/uav/exercise")
@Api(value = "操作练习控制层", tags = "操作练习控制层")
public class UavOperationExerciseController extends BaseController {
    @Resource
    private IUavOperationExerciseService uavOperationExerciseService;

    /**
     * 查询操作练习列表
     */
    @PreAuthorize("@ss.hasPermi('uav:exercise:list')")
    @GetMapping("/list")
    @ApiOperation(value = "查询操作练习列表", notes = "传入uavOperationExercise")
    public TableDataInfo list(UavOperationExercise uavOperationExercise) {
        startPage();
        List<UavOperationExercise> list = uavOperationExerciseService.selectUavOperationExerciseList(uavOperationExercise);
        return getDataTable(list);
    }

    /**
     * 导出操作练习列表
     */
    @PreAuthorize("@ss.hasPermi('uav:exercise:export')")
    @Log(title = "操作练习", businessType = BusinessType.EXPORT)
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
    @GetMapping("/export")
    @ApiOperation(value = "导出操作练习列表", notes = "传入uavOperationExercise")
    public AjaxResult export(UavOperationExercise uavOperationExercise) {
        List<UavOperationExercise> list = uavOperationExerciseService.selectUavOperationExerciseList(uavOperationExercise);
        ExcelUtil<UavOperationExercise> util = new ExcelUtil<UavOperationExercise>(UavOperationExercise.class);
        return util.exportExcel(list, "操作练习数据");
    }

    /**
     * 获取操作练习详细信息
     */
    @PreAuthorize("@ss.hasPermi('uav:exercise:query')")
    @GetMapping(value = "/{operationExerciseId}")
    @ApiOperation(value = "获取操作练习详细信息", notes = "传入operationExerciseId")
    public R<UavOperationExercise> getInfo(@PathVariable("operationExerciseId") Long operationExerciseId) {
        return R.ok(uavOperationExerciseService.getOne(Wrappers.<UavOperationExercise>lambdaQuery().eq(UavOperationExercise::getDelFlag, CommonConstants.DEL_FLAG_OK)
                .eq(UavOperationExercise::getOperationExerciseId,operationExerciseId)));
    }

    /**
     * 新增操作练习
     */
    @PreAuthorize("@ss.hasPermi('uav:exercise:add')")
    @Log(title = "操作练习", businessType = BusinessType.INSERT)
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
    @PostMapping
    @ApiOperation(value = "新增操作练习", notes = "传入uavOperationExercise")
    public R<Integer> add(@RequestBody UavOperationExercise uavOperationExercise) {
        uavOperationExercise.setCreateBy(SecurityUtils.getUsername());
        uavOperationExercise.setCreateTime(DateUtils.getNowDate());
        return R.ok(uavOperationExerciseService.getBaseMapper().insert(uavOperationExercise));
    }

    /**
     * 修改操作练习
     */
    @PreAuthorize("@ss.hasPermi('uav:exercise:edit')")
    @Log(title = "操作练习", businessType = BusinessType.UPDATE)
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
    @PutMapping
    @ApiOperation(value = "修改操作练习", notes = "传入uavOperationExercise")
    public R<Integer> edit(@RequestBody UavOperationExercise uavOperationExercise) {
        if (ObjectUtils.isEmpty(uavOperationExercise.getEndTime())||ObjectUtils.isEmpty(uavOperationExercise.getStartTime())){
            return R.fail("缺少开始时间或结束时间");
        }

        uavOperationExercise.setTrainingDuration(DateUtils.timeDistance(uavOperationExercise.getEndTime(),uavOperationExercise.getStartTime()));
        uavOperationExercise.setUpdateBy(SecurityUtils.getUsername());
        uavOperationExercise.setUpdateTime(DateUtils.getNowDate());
        return R.ok(uavOperationExerciseService.getBaseMapper().updateById(uavOperationExercise));
    }

    /**
     * 删除操作练习
     */
    @PreAuthorize("@ss.hasPermi('uav:exercise:remove')")
    @Log(title = "操作练习", businessType = BusinessType.DELETE)
    @RepeatSubmit(interval = 2000, message = "请求过于频繁")
	@DeleteMapping("/{operationExerciseIds}")
    @ApiOperation(value = "删除操作练习", notes = "传入operationExerciseIds")
    public R<Integer> remove(@PathVariable Long[] operationExerciseIds) {
        return R.ok(uavOperationExerciseService.getBaseMapper().update(null,Wrappers.<UavOperationExercise>lambdaUpdate().in(UavOperationExercise::getOperationExerciseId,operationExerciseIds).eq(UavOperationExercise::getDelFlag,CommonConstants.DEL_FLAG_OK).set(UavOperationExercise::getDelFlag,CommonConstants.DEL_FLAG_DELETED)));
    }
}
